package com.leyou.cart.service;

import com.leyou.auth.pojo.UserInfo;
import com.leyou.cart.client.GoodsClient;
import com.leyou.cart.interceptor.LoginInterceptor;
import com.leyou.cart.mapper.CollMapper;
import com.leyou.cart.pojo.Cart;
import com.leyou.cart.pojo.Collection;
import com.leyou.common.utils.JsonUtils;
import com.leyou.item.pojo.Sku;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.BoundHashOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class CartService {
    /*@Autowired
    private CollMapper collMapper;*/
    @Autowired
    private GoodsClient goodsClient;
    @Autowired
    private StringRedisTemplate redisTemplate;


    private static final String KEY_PREFIX = "leyou:cart:";

    public void saveCart(Cart cart) {
        // 获取用户信息
        UserInfo userInfo = LoginInterceptor.get();
        // 处理key
        String key = KEY_PREFIX + userInfo.getId();
        // 查询该用户的购物车信息
        BoundHashOperations<String, Object, Object> hashOperations = this.redisTemplate.boundHashOps(key);

        // 取出购物数量
        Integer num = cart.getNum();

        if (hashOperations.hasKey(cart.getSkuId().toString())){
            // 有，更新数量
            String cartJson = hashOperations.get(cart.getSkuId().toString()).toString();
            cart = JsonUtils.parse(cartJson, Cart.class);
            cart.setNum(cart.getNum() + num);
        } else {
            // 无，新增购物车
            cart.setUserId(userInfo.getId());
            // 查询sku
            Sku sku = this.goodsClient.querySkuById(cart.getSkuId());
            cart.setTitle(sku.getTitle());
            cart.setOwnSpec(sku.getOwnSpec());
            cart.setImage(StringUtils.isBlank(sku.getImages()) ? "" : StringUtils.split(sku.getImages(), ",")[0]);
            cart.setPrice(sku.getPrice());
        }
        // 更新redis购物车
        hashOperations.put(cart.getSkuId().toString(), JsonUtils.serialize(cart));
    }

    public List<Cart> queryCarts() {
        UserInfo userInfo = LoginInterceptor.get();
        String key = KEY_PREFIX + userInfo.getId();
        BoundHashOperations<String, Object, Object> hashOperations = this.redisTemplate.boundHashOps(key);
        // 获取购物车字符串集合
        List<Object> cartJsons = hashOperations.values();
        // 购物车字符串集合为空，直接返回null
        if (CollectionUtils.isEmpty(cartJsons)){
            return null;
        }
        // 把List<Object>集合处理成List<Cart>集合
        return cartJsons.stream().map(cartJson -> JsonUtils.parse(cartJson.toString(), Cart.class)).collect(Collectors.toList());
    }
    /*public int addColl(Long skuId) {
        // 获取用户信息
        UserInfo userInfo = LoginInterceptor.get();
        Collection recode = new Collection();
        recode.setUserId(userInfo.getId());
        recode.setSpuId(skuId);
        return collMapper.insertSelective(recode);
    }
    public  List<Sku> queryColl() {
        // 获取用户信息
        UserInfo userInfo = LoginInterceptor.get();
        return collMapper.queryColl(userInfo.getId());
    }*/

    public Boolean deleteCart(Long skuId) {
        // 获取用户信息
        UserInfo userInfo = LoginInterceptor.get();
        String key = KEY_PREFIX + userInfo.getId();
        return redisTemplate.delete(key);
    }
}
